package cn.edu.scau.sec.rw.model.pro_manage;

import cn.edu.scau.sec.rw.model.pro_manage.simulation.CPU;

/**
 * 进程控制
 */
public class ProcessControl {

    /**
     * 创建进程，将进程加入空白队列
     * @return
     */
    public Process creatProcess() {
        Process process = new Process();
        Process.getInitProcesses().add(process);
        return process;
    }

    /**
     * 销毁指定进程，回收其PCB，并将其从进程队列中移除
     * @param process
     */
    public void destroyProcess(Process process) {
        process.getPCB().getId().add(process.getPCB().getProId());
        process.getPCB().getPcbList().remove(process.getPCB());
        Process.getInitProcesses().remove(process);
    }

    /**
     * 阻塞指定进程，保存进程的CPU现场，修改进程状态，并加入阻塞队列
     * @param process
     */
    public void blockProcess(Process process) {
        //process.getPCB().setRegister(cpu.getRegister());
        //Process.getInitProcesses().remove(cpu.getProcess());
        Process.getBlockProcesses().add(process);
        process.getPCB().setProState("阻塞");
    }

    public void startProcess(Process process) {
        process.getPCB().setProState("就绪");
        Process.getReadyProcesses().add(process);
        Process.getInitProcesses().remove(process);
    }

    public void awakeProcess(Process process) {
        process.getPCB().setProState("就绪");
        if (process.getClock().getRelativeClock() == 0) {
            process.getClock().setRelativeClock(6);
        }
        Process.getReadyProcesses().add(process);
        Process.getBlockProcesses().remove(process);
    }
}
